﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService

<CnGalWebSite.Components.Containers.MasaTitleContainer Outline Title="随机评价" Icon="@IconType.GameRecord.ToIconString()" Class="mb-4">
    <ButtonContent>
        <MButton Rounded  OnClick="OnClickMore" Color="primary">
            <MIcon Left>mdi-refresh  </MIcon>
            换一批
        </MButton>
    </ButtonContent>
    <ChildContent>
        <MSlideGroup ShowArrows="true" Class="slide-group-garp">
            <ChildContent>
                @foreach (var item in _dataCacheService.RandomUserScoresCatche.Take(12))
                {
                    <MSlideItem>
                        <div style="width:400px;">
                            <CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.UserScoreCard Model="item" GameName="@item.GameName" OnClick="@(()=>OnClickUser(item))" IsDubbing="item.IsDubbing" Clickable />
                        </div>
                       
                    </MSlideItem>
                }
            </ChildContent>

        </MSlideGroup>
    </ChildContent>

</CnGalWebSite.Components.Containers.MasaTitleContainer>

<CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.UserScoreDialog @ref="userScoreDialog" />
@code {

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.UserScoreDialog userScoreDialog;

    protected override async Task OnInitializedAsync()
    {

        await Refresh();
    }


    public async Task Refresh()
    {
        try
        {
            if (_dataCacheService.RandomUserScoresCatche.Count == 0)
            {
                _dataCacheService.RandomUserScoresCatche = await _httpService.GetAsync<List<PlayedGameUserScoreRandomModel>>("api/playedgame/GetRandomUserScores");
            }
            _dataCacheService.RandomUserScoresCatche.Random();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取随机用户评分列表失败");
        }
    }

    public void OnClickMore()
    {
        _dataCacheService.RandomUserScoresCatche.Random();
        StateHasChanged();
        
    }


    public void OnClickUser(PlayedGameUserScoreRandomModel model)
    {
        if (userScoreDialog != null)
        {
            userScoreDialog.Refresh(true, model, model.GameName, model.GameId, model.IsDubbing);
        }
    }
}
